package com.example.dell.newstart.res;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;


import com.example.dell.newstart.R;
import com.example.dell.newstart.base.BaseActivity;
import com.example.dell.newstart.base.RequestInterface;
import com.example.dell.newstart.base.ServerResponse;
import com.example.dell.newstart.login.Login;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class LogActivity extends BaseActivity {
    public static final String TAG = LogActivity.class.getName();

    private String str1 = "";
    private EditText username;
    private EditText password;
    private EditText password2;
    private EditText phone;
    private Button button;
    private String str2 = "";
    private String str = "";
    private String str3 = "";
    private Code verifyCode;
    private EditText et_phoneCode;
    private ImageView image;
    String realCode;
    private Button toBtn;
    //private Button reg;



    @Override
    public int initLayout() {

        return R.layout.res_layout;
    }

    @Override
    public void initView() {
        password = findViewById(R.id.et_password);
        username = findViewById(R.id.username);
        password2 = findViewById(R.id.et_password2);
        phone = findViewById(R.id.et_phone);
        et_phoneCode = (EditText) findViewById(R.id.et_phoneCodes);
        button = (Button) findViewById(R.id.login_btn);
        image = findViewById(R.id.iv_showCode);
        button=findViewById(R.id.login_btn);
        toBtn=findViewById(R.id.toLogin);
        //获取工具类生成的图片验证码对象
        image.setImageBitmap(Code.getInstance().createBitmap());
        realCode = Code.getInstance().getCode().toLowerCase();
        image.setOnClickListener(new View.OnClickListener() {
            //给注册按钮设置监听
            public void onClick(View v) {
                image.setImageBitmap(Code.getInstance().createBitmap());
                realCode = Code.getInstance().getCode().toLowerCase();
                Log.v(TAG, "realCode" + realCode);
            }
        });
        toBtn.setOnClickListener(new View.OnClickListener() {
            //给注册按钮设置监听
            public void onClick(View v) {
                Intent intent =new Intent(LogActivity.this,Login.class);

                startActivity(intent);

            }
        });


//        reg.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(LogActivity.this, LoginActivity.class);
//                startActivity(intent);
//            }
//
//
//        });

        //监听button事

        button.setOnClickListener(new View.OnClickListener() {
            //给注册按钮设置监听
            public void onClick(View v) {
                str = username.getText().toString().trim();
                str1 = password.getText().toString().trim();//第一次输入的密码赋值给password
                str2 = password2.getText().toString().trim();//第二次输入的密码赋值给password
                str3 = phone.getText().toString().trim();
                String phoneCode = et_phoneCode.getText().toString().toLowerCase();

                //插入ContentValues中的数据
                if (str1.equals(str2) && phoneCode.equals(realCode)) {
                    doRegister( str,str1,str3);
                    Intent intent =new Intent(LogActivity.this,Login.class);

                    startActivity(intent);



                } else if(str1.equals(str2) == false) {
                    Toast.makeText(getApplication(), "    密码不一致，请重新输入", Toast.LENGTH_SHORT).show();

                }
                else if (phoneCode.equals(realCode) == false) {
                    Toast.makeText(getApplication(), " 验证码错误，请重新输入", Toast.LENGTH_SHORT).show();
                }
            }

        });

    }


    public void onclick(View  view){




    }

    @Override
    public void initData() {

    }

    private void doRegister(String username,String password,String phone) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(RequestInterface.basePath)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        RequestInterface.Register requestInterfaceLogin = retrofit.create(RequestInterface.Register.class);
        Call<ServerResponse> call = requestInterfaceLogin.register(username, password, phone);
        call.enqueue(new Callback<ServerResponse>() {


            public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
                /**
                 * response.body()获取到返回的ServerResponse，再通过获取Status判断是否
                 * 登陆成功/请求数据成功
                 */
                if (response.body() == null) {
                    toastShort("未获取到相关数据");
                    return;
                }
                //log(response.body().getMsg());
                if (response.body().getStatus() == 0) {
                    //获取返回的数据（就是泛型ServerResponse中的T所代表的数据）
                    toastShort("注册成功");

                    goNextActivity(Login.class);
                    finish();
                } else {
                    toastShort("注册失败");
                }
            }


            public void onFailure(Call<ServerResponse> call, Throwable t) {
                Log.d(TAG, "onFailure: ");
                toastShort("注册失败.！");
            }
        });
    }
}







//package com.example.dell.newstart.res;
//
//import android.content.Intent;
//import android.util.Log;
//import android.view.View;
//import android.widget.Button;
//import android.widget.EditText;
//import android.widget.ImageView;
//import android.widget.Toast;
//
//
//import com.example.dell.newstart.R;
//import com.example.dell.newstart.base.BaseActivity;
//import com.example.dell.newstart.base.RequestInterface;
//import com.example.dell.newstart.base.ServerResponse;
//import com.example.dell.newstart.login.LoginActivity;
//import com.example.dell.newstart.res.Code;
//
//import retrofit2.Call;
//import retrofit2.Callback;
//import retrofit2.Response;
//import retrofit2.Retrofit;
//import retrofit2.converter.gson.GsonConverterFactory;
//
//
//public class LogActivity extends BaseActivity {
//    public static final String TAG = LogActivity.class.getName();
//
//    private String str1;
//    private EditText username;
//    private EditText password;
//    private EditText password2;
//    private Button button;
//    private String str2;
//    private String str ;
//    private Code verifyCode;
//    private EditText et_phoneCode;
//    String phoneCode = "";
//    private ImageView image;
//    String realCode;
//    private Button toBtn;
//
//    //private Button reg;
//
//
//    @Override
//    public int initLayout() {
//
//        return R.layout.res_layout;
//    }
//
//    @Override
//    public void initView() {
//        password = findViewById(R.id.et_password);
//        username = findViewById(R.id.username);
//        password2 = findViewById(R.id.et_password2);
//        et_phoneCode = (EditText) findViewById(R.id.et_phoneCodes);
//        button = (Button) findViewById(R.id.login_btn);
//        image = findViewById(R.id.iv_showCode);
//        button = findViewById(R.id.login_btn);
//        toBtn = findViewById(R.id.toLogin);
//        //获取工具类生成的图片验证码对象
//        button.setOnClickListener((View.OnClickListener) this);
//
//        image.setImageBitmap(Code.getInstance().createBitmap());
//        realCode = Code.getInstance().getCode().toLowerCase();
//        image.setOnClickListener(new View.OnClickListener() {
//            //给注册按钮设置监听
//            public void onClick(View v) {
//                image.setImageBitmap(Code.getInstance().createBitmap());
//                realCode = Code.getInstance().getCode().toLowerCase();
//                Log.v(TAG, "realCode" + realCode);
//            }
//        });
//        toBtn.setOnClickListener(new View.OnClickListener() {
//            //给注册按钮设置监听
//            public void onClick(View v) {
//                Intent intent = new Intent(LogActivity.this, LoginActivity.class);
//
//                startActivity(intent);
//
//            }
//        });
//
//
////        reg.setOnClickListener(new View.OnClickListener() {
////            @Override
////            public void onClick(View v) {
////                Intent intent = new Intent(LogActivity.this, LoginActivity.class);
////                startActivity(intent);
////            }
////
////
////        });
//
//        //监听button事
//
////        button.setOnClickListener(new View.OnClickListener() {
//////给注册按钮设置监听
////public void onClick(View v) {
////        str = username.getText().toString().trim();
////        str1 = password.getText().toString().trim();//第一次输入的密码赋值给password
////        str2 = password2.getText().toString().trim();//第二次输入的密码赋值给password
////        String phoneCode = et_phoneCode.getText().toString().toLowerCase();
////
////        //插入ContentValues中的数据
////        if (str1.equals(str2) && phoneCode.equals(realCode)) {
////            doRegister( str,str1);
////           Intent intent =new Intent(LogActivity.this,LoginActivity.class);
////
////           startActivity(intent);
////
////
////
////        } else if (phoneCode.equals(realCode) == false) {
////        Toast.makeText(getApplication(), " 验证码错误，请重新输入", Toast.LENGTH_SHORT).show();
////        } else if(str1.equals(str2) == false) {
////        Toast.makeText(getApplication(), "    密码不一致，请重新输入", Toast.LENGTH_SHORT).show();
////
////        }
////        }
////
////        });
//
//    }
//
//
//    public void onViewClicked() {
//
//        str = username.getText().toString().trim();
//        str1 = password.getText().toString().trim();//第一次输入的密码赋值给password
//        str2 = password2.getText().toString().trim();//第二次输入的密码赋值给password
//        phoneCode = et_phoneCode.getText().toString().trim();
//        doRegister();
//    }
//
//
//    @Override
//    public void initData() {
//
//    }
//
//    private void doRegister() {
//        Retrofit retrofit = new Retrofit.Builder()
//                .baseUrl(RequestInterface.basePath)
//                .addConverterFactory(GsonConverterFactory.create())
//                .build();
//
//        RequestInterface.Register requestInterfaceLogin = retrofit.create(RequestInterface.Register.class);
//
//        Call<ServerResponse> call = requestInterfaceLogin.register(str,str1);
//        call.enqueue(new Callback<ServerResponse>() {
//
//
//            public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
//                /**
//                 * response.body()获取到返回的ServerResponse，再通过获取Status判断是否
//                 * 登陆成功/请求数据成功
//                 */
//                if (response.body() == null) {
//                    toastShort("未获取到相关数据");
//                    return;
//                }
//                //log(response.body().getMsg());
//                if (response.body().getStatus() == 0 ) {
//
//                    //获取返回的数据（就是泛型ServerResponse中的T所代表的数据）
//                    toastShort("注册成功");
//                    if ((str1.equals(str2)) && phoneCode.equals(realCode) ) {
//
//                        goNextActivity(LoginActivity.class);
//                        finish();
//
//                    }
//
//
//
//                } else if (phoneCode.equals(realCode) == false) {
//                    Toast.makeText(getApplication(), " 验证码错误，请重新输入", Toast.LENGTH_SHORT).show();
//                } else if (str1.equals(str2) == false) {
//                    Toast.makeText(getApplication(), "    密码不一致，请重新输入", Toast.LENGTH_SHORT).show();
//
//                } else {
//                    toastShort("注册失败");
//                }
//            }
//
//            public void onFailure(Call<ServerResponse> call, Throwable t) {
//                Log.d(TAG, "onFailure: ");
//                toastShort("注册失败.！");
//            }
//        });
//
//
//    }
//}
//
//
//
